package com.lovo.pis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.pis.dto.LogBean;
import com.lovo.pis.dto.PageBean;
import com.lovo.pis.dto.RoleBean;
import com.lovo.pis.dto.UserBean;
import com.lovo.pis.mapper.LogMapper;
import com.lovo.pis.mapper.UserMapper;
import com.lovo.pis.service.IUserService;
import com.lovo.pis.utils.JwtUtil;
import com.lovo.pis.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserBean> implements IUserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    LogMapper logMapper;
    @Autowired
    LogUtil logUtil;

    @Override
    public IPage<UserBean> queryAll(PageBean<UserBean> pageInfo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (pageInfo.getData().getUsername() != null && pageInfo.getData().getUsername() != "") {
            queryWrapper.like("username", pageInfo.getData().getUsername());
        }
        return userMapper.selectPage(new Page<>(pageInfo.getCurPage(), pageInfo.getPageSize()), queryWrapper);
    }

    @Override
    public List<UserBean> queryAllUser() {
        return userMapper.selectList(null);
    }


    @Override
    @Transactional
    public boolean removeById(int id) {
        //删除关联表信息
        userMapper.deleteRole(id);
        //删除用户表用户
        userMapper.deleteById(id);
        return true;
    }

    @Override
    @Transactional
    public boolean addUser(UserBean userBean) {
        //添加用户
        userMapper.insert(userBean);
        int userId = userBean.getId();
        //添加关联表信息
        List<Integer> list = new ArrayList<>();
        for (RoleBean roleBean : userBean.getRoleList()) {
            list.add(roleBean.getId());
        }
        userMapper.insertRole(userId, list);
        return true;
    }

    @Override
    @Transactional
    public boolean changeRole(UserBean userBean) {
        //清除角色
        userMapper.deleteRole(userBean.getId());
        //添加
        List<Integer> list = new ArrayList<>();
        for (RoleBean roleBean : userBean.getRoleList()) {
            list.add(roleBean.getId());
        }
        userMapper.insertRole(userBean.getId(), list);
        return true;
    }


    @Override
    public int changePwd(int id, String oldPwd, String newPwd, String repNewPwd) {
        UserBean userBean = userMapper.selectById(id);
        UpdateWrapper updateWrapper = new UpdateWrapper();
        if (oldPwd.equals(userBean.getPassword())) {
            if (Objects.equals(newPwd, repNewPwd)) {
                updateWrapper.eq("id", id);
                userBean.setPassword(newPwd);
                userMapper.update(userBean, updateWrapper);
                return 2;
            }else {
                return 1;
            }
        }
        return 0;
    }

    @Override
    public UserBean login(HttpServletResponse response, String username, String password) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);
        UserBean userBean = userMapper.selectOne(queryWrapper);
        if (userBean != null) {
            Map<String, String> userMap = new HashMap<>() {{
                put("id", String.valueOf(userBean.getId()));
                put("username", username);
                put("password", password);
                put("realName", userBean.getRealName());
            }};
            String token = JwtUtil.getToken(userMap);
            response.addHeader("token", token);
            logUtil.recordLog(userBean.getId(), "登陆了系统");
            return userBean;
        } else {
            return null;
        }

    }

    @Override
    public UserBean queryById(int id) {
        UserBean userBean = userMapper.selectAll(id);
        return userBean;
    }

    @Override
    public UserBean queryRealName(int id) {
        return userMapper.selectById(id);
    }


}
